
cd "/Users/kevin/Dropbox/TZ ITNs"

set more off
set varabbrev off

***********
*consumption data, created by NPS team
***********

use "2008-09 data/TZNPS1_consdta/TZY1.HH.Consumption.dta", clear

destring hhid, replace

save "constructed data/2008/consumption08.dta", replace

***********
*survey details (from modules A and T of the qnr)
***********

use "2008-09 data/TZNPSHH1DTA_E/SEC_A_T.dta", clear

destring hhid, replace

merge 1:1 hhid using "constructed data/2008/consumption08.dta", nogen

save "constructed data/2008/SEC_A_T_consumption.dta", replace

****************************
*merge official approval data (H2 section of qnr)
***************************

use "2008-09 data/TZNPSHH1DTA_E/SEC_H2.dta", clear

destring hhid, replace

gen shcode1=shcode
replace shcode1="1" if shcode=="A"
replace shcode1="2" if shcode=="B"
replace shcode1="3" if shcode=="C"
replace shcode1="4" if shcode=="D"
replace shcode1="5" if shcode=="E"
replace shcode1="6" if shcode=="F"
replace shcode1="7" if shcode=="G"
replace shcode1="8" if shcode=="H"

destring shcode1, replace
drop shcode

reshape wide shq4 shq5 shq6 shq7, i(hhid) j(shcode1)

save "constructed data/2008/SEC_H2", replace


***********
**merge in file with questionnaire section H1 (part of politics module)
***********

use "2008-09 data/TZNPSHH1DTA_E/SEC_H1_J_K2_O2_P1_Q1_S1.dta", clear

destring hhid, replace
sort hhid

save "constructed data/2008/SEC_H1_J_K2_O2_P1_Q1_S1.dta", replace


*******
*merge the consumption/survey details modules with governance modules
*rename and clean governance module variables
*******

merge 1:1 hhid using "constructed data/2008/SEC_A_T_consumption.dta", nogen

merge 1:1 hhid using "constructed data/2008/SEC_H2", nogen

rename (shq1 shq2 shq3) (respondent_id selection_followed replacement_respondent)

rename (shq41 shq61 shq71 shq42 shq62 shq72  shq43 shq63 shq73 shq44 shq64 shq74 shq48 shq68 shq78) ///
(vc approve_vc vote_vc veo approve_veo vote_veo weo approve_weo vote_weo ///
 councillor approve_councillor vote_councillor mp  approve_mp vote_mp)

foreach var in vote_vc vote_councillor vote_mp {
gen `var'1=`var'
replace `var'1=0 if `var'1==2
}

foreach var in vote_vc1 vote_councillor1 vote_mp1 {
replace `var'=. if `var'==3
replace `var'=. if `var'==9
}

foreach var in approve_vc  approve_councillor approve_veo approve_weo approve_mp {
gen `var'1= (`var'<=2) if !missing(`var')
replace `var'1=. if `var'1==5 | `var'==9
}

destring hhid, replace

save "constructed data/2008/political_consumption_H3.dta", replace

**********
**main household roster data file
**********

use "2008-09 data/TZNPSHH1DTA_E/SEC_B_C_D_E1_F_G1_U.dta", clear 
gen roster_no=sbmemno
 
destring hhid, replace
sort hhid 
 
save "constructed data/2008/roster_merge", replace

*******
**now merge the roster data with the (previous) consumption and governance data file
*also create bed net variables
*******

*Have created a unique id for 20 individual roster respondents, gov_id (hhid + sbmemno)
use "constructed data/2008/political_consumption_H3.dta", clear

merge 1:m hhid using "constructed data/2008/roster_merge", nogen

replace sdq17=. if sdq17==5 
rename sdq17 net
lab var net "type of net slept under if any"
gen slept_net= (net<=3) if net!=.
lab var slept_net "respondent slept under any net"
gen slept_itn= (net==2 | net==3) if net!=.
lab var slept_itn "respondent slept under treated net"
replace sdq18=. if sdq18==5
rename sdq19 price_net 
gen free_net= (sdq18==1) if !missing(sdq18)
gen free_net_all= (sdq18==1) 
lab var free_net "previously mentioned net was acquired for free" 
lab var free_net_all "any net acquired for free" 
gen voucher_net= (sdq18==3) if !missing(sdq18)
lab var voucher_net "previously mentioned net was acquired with voucher"
gen free_voucher= (free_net==1 | voucher_net==1) if !missing(free_net) & !missing(voucher_net)
gen free_itn= (sdq18==1) & (net==2 | net==3) if !missing(net) & !missing(sdq18)
gen free_itn_all= (sdq18==1) & (net==2 | net==3)
bys hhid: egen anynet=max(slept_net) // alt measure of ownership could come from asset index
lab var anynet "any hh member slept under net"
bys hhid: egen free_hh= max(free_net) if !missing(free_net)
bys hhid: egen free_hh_all= max(free_net_all)
lab var free_hh "any hh member slept under free net"
bys hhid: egen freevoucher_hh= max(free_voucher) if !missing(free_voucher)
lab var freevoucher_hh "any hh member slept under free/voucher net"
bys hhid: egen voucher_hh=max(voucher_net) if !missing(voucher_net)
bys hhid: egen free_itnhh=max(free_itn) if !missing(free_itn)
lab var free_itnhh "any hh member slept under free treated net"
bys hhid: egen anyslept_itn=max(slept_itn)

save "constructed data/2008/political_consumption_roster_H3.dta", replace 

use "2008-09 data/TZNPS1CMDTA_E/SEC_H.dta", clear // *start with section H, which has info on leader party, etc

rename (ch01 ch06 ) (leader_type leader_party)
gen leader_kitongogi= (leader_type<=5)
gen leader_village= (leader_type==7 | leader_type==8) if !missing(leader_type)
gen leader_weo= (leader_type==9) 

gen ccm= (leader_party==1) if !missing(leader_party)
label var ccm "village official party is CCM"
gen chadema= (leader_party==2) if !missing(leader_party)
label var chadema "village official party is Chadema"
gen cuf= (leader_party==3) if !missing(leader_party)
label var cuf "village official party is CUF"
gen party_other= (leader_party==4) if !missing(leader_party)
gen opposition= (cuf==1) | (chadema==1) | (party_other==1) if !missing(leader_party)

gen cluster_08=ea_id
destring cluster_08, replace 
sort ea_id 
save "constructed data/2008/communityH_replication.dta", replace

use "2008-09 data/TZNPS1CMDTA_E/SEC_A.dta", clear
sort ea_id
merge 1:1 ea_id using "constructed data/2008/communityH_replication.dta", nogen

save "constructed data/2008/communityAH.dta", replace

use "constructed data/2008/political_consumption_roster_H3.dta", replace 

*generate EA id for merging
foreach var in region district ward ea {
bys hhid: egen `var'1=max(`var')
}

gen str2 region2 = string(region1,"%02.0f") 
gen str2 district2 = string(district1,"%02.0f") 
gen str3 ward2 = string(ward1, "%03.0f")
gen str3 ea2 = string(ea1, "%03.0f")

egen ea_id=concat(region2 district2 ward2 ea2)

merge m:1 ea_id using "constructed data/2008/communityAH.dta", nogen

tostring hhid, gen(hhid_2008_temp) format(%14.0f)
tostring hhid, gen(hhid_2008_alt) format(%014.0f)

gen hhid_2008=hhid_2008_temp if region>=10
replace hhid_2008=hhid_2008_alt if region<10

gen respondent_2008= (respondent_id==roster_no)
gen ccm_vote= (vote_vc1==1 & ccm==1) if !missing(vote_vc1) & !missing(ccm==1)

keep ccm opposition approve_vc1 approve_veo1 approve_councillor1 approve_weo1 approve_mp1 ///
free_itnhh free_net_all free_itn_all ccm_vote ///
vote_vc1 vote_councillor1 vote_mp1 hhid_2008
rename (ccm ccm_vote) (ccm_lag ccm_vote_lag)
rename (approve* free* vote* opposition)  (approve*_lag  free*_lag vote*_lag opposition_lag)

*keep one observation per household for merging with 2010-2011 data
bys hhid_2008: gen index=_n
drop if index>1 

drop index

save "constructed data/2008/vill_pol_for_2010_merge_prep.dta", replace

